package IC.Parser;

public class TestLexer extends junit.framework.TestCase {
	
	private void Diff(String fileName, String tokenFileName)
	{
		java.io.BufferedReader tokenFile;
		String expectedToken = null;
		System.out.println("\n" + fileName + "\n");
		
		try 
		{	
			java.io.Reader reader = new java.io.FileReader("/a/home/cc/students/cs/iliagore/Compilation/Project/tests/" + fileName);
			tokenFile = new java.io.BufferedReader(
							new java.io.FileReader(
									"/a/home/cc/students/cs/iliagore/Compilation/Project/tests/" + tokenFileName));
			Lexer lexer = new Lexer(reader);
			
			while ((expectedToken = tokenFile.readLine()) != null) 
			{
				String quote = "";
				Token currToken = lexer.next_token();
				if(currToken.GetId() == 43)
					quote = "\"";
	               
				String token = currToken.GetLine() + 1
					+ ": " + currToken.ToString()
					+ (currToken.GetValue() != null 
							? "(" + quote + currToken.value.toString()+ quote + ")" : "");
				assertEquals(expectedToken, token);
				System.out.println(token);
			}
		} 
		catch(java.io.IOException e)
		{
			System.out.println("Lexical Error, cought IOException with message: " + e.getMessage());
			fail();
	    } 
		catch (LexicalError e) 
		{
			assertEquals(expectedToken, e.getMessage());
			System.out.println(e);
		}
	    catch(java.lang.Exception e)
		{
			System.out.println("Caught unhandled java.lang.exception, with message: " + e.getMessage());
		}
	}

	public void testQuickSort() 
	{
		Diff("Quicksort.ic", "Quicksort.tokens.txt");
	}
	
	public void testSieve() 
	{
		Diff("Sieve.ic", "Sieve.tokens.txt");
	}
	
	public void testFarmersLife() 
	{
		Diff("FarmersLife.ic", "FarmersLife.tokens.txt");
	}
	
	public void testBubbleSort() 
	{
		Diff("BubbleSort.ic", "BubbleSort.tokens.txt");
	}
	
	public void testBadQuickSort() 
	{
		Diff("Quicksort_bad.ic", "Quicksort_bad.tokens.txt");
	}
	
	public void testIllegalEscapeSequence() 
	{
		Diff("IllegalEscapeSequence.ic", "IllegalEscapeSequence.tokens.txt");
	}
	
	public void testIllegalIdentifier() 
	{
		Diff("IllegalIdentifier.ic", "IllegalIdentifier.tokens.txt");
	}
	
	public void testIntegerOutOfBounds() 
	{
		Diff("IntegerOutOfBounds.ic", "IntegerOutOfBounds.tokens.txt");
	}
	
	public void testLeadingZeroes() 
	{
		Diff("LeadingZeroes.ic", "LeadingZeroes.tokens.txt");
	}
	
	public void testUnclosedComment() 
	{
		Diff("UnclosedComment.ic", "UnclosedComment.tokens.txt");
	}
	
	public void testUnclosedString() 
	{
		Diff("UnclosedString.ic", "UnclosedString.tokens.txt");
	}

}
